home *** CD-ROM | disk | FTP | other *** search
- /*
- * Blob Manager Demonstration: Fox, Goose and Grain module
- *
- * 1 July 1986 Paul DuBois
- */
-
- # include "TransSkel.h"
-
-
- # include "BlobMgr.h"
- # include "BlobDemo.h"
-
-
- # define left 0 /* beginning indices of blobs on each river bank */
- # define right 3
-
-
- /*
- * positional constants - as yet woefully incomplete
- */
-
- # define xMid 185 /* x-midpoint of screen */
- # define yOffMesg 110 /* y-offset of status message */
- # define yOffBox 160 /* y-offset of text box */
-
-
- static WindowPtr wind;
- static BlobSetHandle donors;
- static BlobSetHandle receptors;
- static BlobHandle fox;
- static BlobHandle goose;
- static BlobHandle grain;
- static BlobHandle whew;
- static BlobHandle rats;
- static BlobHandle farmer;
- static short bank;
-
- static ControlHandle moveCtl;
- static ControlHandle restartCtl;
- static Boolean wait;
- static Str255 statusStr = "\p";
-
-
- static void
- StatusMesg (StringPtr s)
- {
- Rect r;
-
- SetRect (&r, xMid - 90, yOffMesg, xMid + 90, yOffMesg + 20);
- TextBox (s+1, (long) s[0], &r, teJustCenter);
- StrCpy (statusStr, s);
- }
-
-
- static void
- GameOver (StringPtr msg, BlobHandle g)
- {
- wait = true;
- HideControl (moveCtl);
- ShowControl (restartCtl);
- StatusMesg (msg);
- GlueGlob (g, farmer);
- }
-
-
- static void
- FreezeBank (short bank)
- {
- short i;
-
- for (i = bank; i < bank + 3; ++i)
- FreezeBlob (GetBlobHandle (receptors, i));
- }
-
-
- static void
- Restart (void)
- {
- StatusMesg ("\p");
- UnglueGlobSet (receptors);
- ThawBlobSet (receptors);
- MoveBlob (farmer, inFullBlob, 100, 63);
- bank = left;
- GlueGlob (fox, GetBlobHandle (receptors, left));
- GlueGlob (goose, GetBlobHandle (receptors, left+1));
- GlueGlob (grain, GetBlobHandle (receptors, left+2));
- FreezeBank (right);
- HideControl (restartCtl);
- SetCTitle (moveCtl, "\pMove Right");
- ShowControl (moveCtl);
- wait = false;
- /*Update (false);*/
- InvalRect (&wind->portRect);
- }
-
-
- static short
- LookFor (short bank, short i)
- {
- BlobHandle b;
- short j;
-
- for (j = bank; j < bank + 3; j++)
- {
- b = GetBlobHandle (receptors, j);
- if (GetBRefCon (BGlob (b)) == i)
- {
- switch (i)
- {
- case gooseIconRes:
- GameOver ("\pYour goose is cooked!", rats);
- return (1);
- case grainIconRes:
- GameOver ("\pToo bad about that grain!", rats);
- return (1);
- }
- }
- }
- return (0);
- }
-
-
- /*
- * Look for pairs that can't be together alone on a bank
- */
-
- static void
- CheckStatus (short bank)
- {
- BlobHandle b;
- short i;
-
- for (i = bank; i < bank + 3; i++)
- {
- b = GetBlobHandle (receptors, i);
- switch (GetBRefCon (BGlob (b)))
- {
- case foxIconRes:
- if (LookFor (bank, gooseIconRes))
- return;
- break;
- case gooseIconRes:
- if (LookFor (bank, grainIconRes))
- return;
- break;
- }
- }
- }
-
-
- /*
- * These are created disabled so they aren't drawn. (Some of them are
- * glued strictly programmatically and are not normally displayed.)
- */
-
- static BlobHandle
- MakeDonor (short iconNum)
- {
- BlobHandle b;
- Handle h;
- Rect r;
-
- b = NewBlob (donors, false, 1, false, (long) iconNum);
- SetRect (&r, 0, 0, 32, 32);
- h = GetIcon (iconNum);
- OpenBlob ();
- PlotIcon (&r, h);
- CloseRectBlob (b, &r, &r);
- return (b);
- }
-
-
- static void
- MakeFarmerBlob (void)
- {
- Rect r, r2;
- Handle h;
-
- farmer = NewBlob (receptors, true, 0, false, 0L);
- SetRect (&r, 0, 0, 70, 36);
- SetRect (&r2, 2, 2, 34, 34);
- h = GetIcon (farmerIconRes);
- OpenBlob ();
- FrameRect (&r);
- EraseRect (&r2);
- FrameRect (&r2);
- OffsetRect (&r2, 34, 0);
- PlotIcon (&r2, h);
- OffsetRect (&r2, -34, 0);
- CloseRectBlob (farmer, &r2, &r); /* will be moved by FggRestart */
- MoveBlob (farmer, inFullBlob, 100, 63);
- }
-
-
- static void
- MakeBankBlobs (short bank, short xPos)
- {
- BlobHandle b;
- Rect r;
- short i;
-
- for (i = bank; i < bank + 3; ++i)
- {
- b = NewBlob (receptors, true, 0, false, 0L);
- SetRect (&r, 0, 0, 32, 32);
- OffsetRect (&r, xPos, (short) 30 + (i-bank)*35);
- OpenBlob ();
- EraseRect (&r);
- FrameRect (&r);
- CloseRectBlob (b, &r, &r);
- }
- }
-
-
- static pascal void
- Mouse (Point pt, long t, short mods)
- {
- BlobHandle b;
- ControlHandle ctl;
- short i, otherBank;
-
- if (FindControl (pt, wind, &ctl))
- {
- if (TrackControl (ctl, pt, nil))
- {
- if (ctl == restartCtl)
- Restart ();
- else if (ctl == moveCtl)
- {
- otherBank = bank;
- if (bank == left)
- {
- bank = right;
- MoveBlob (farmer, inFullBlob, 125, 63);
- MoveBlob (farmer, inFullBlob, 150, 63);
- MoveBlob (farmer, inFullBlob, 175, 63);
- MoveBlob (farmer, inFullBlob, 200, 63);
- SetCTitle (moveCtl, "\pMove Left");
- }
- else
- {
- bank = left;
- MoveBlob (farmer, inFullBlob, 175, 63);
- MoveBlob (farmer, inFullBlob, 150, 63);
- MoveBlob (farmer, inFullBlob, 125, 63);
- MoveBlob (farmer, inFullBlob, 100, 63);
- SetCTitle (moveCtl, "\pMove Right");
- }
- ThawBlobSet (receptors);
- FreezeBank (otherBank);
- ValidRect (&wind->portRect);
- CheckStatus (otherBank);
- }
- }
- }
- if (wait) return;
- BlobClick (pt, t, donors, receptors);
-
- for (i = right; i < right + 3; ++i) /* check if all transported */
- {
- if (BGlob (GetBlobHandle (receptors, i)) == nil)
- return; /* nope */
- }
- GameOver ("\pMission Accomplished.", whew);
- }
-
-
- static pascal void
- Update (Boolean resized)
- {
- Rect r;
-
- DrawControls (wind);
- DrawBlobSet (receptors);
- StatusMesg (statusStr);
- MoveTo (90, 10);
- LineTo (90, 140);
- MoveTo (279, 10);
- LineTo (279, 140);
- MoveTo (15, 15);
- DrawString ("\pLeft Bank");
- MoveTo (290, 15);
- DrawString ("\pRight Bank");
- }
-
-
- static pascal void
- Activate (Boolean active)
- {
- if (active)
- {
- SetDragRects (wind);
- SetBCPermissions (false, true, false, true, true);
- }
- }
-
-
- void
- FggInit (void)
- {
- Rect r;
-
- SkelWindow (wind = GetDemoWind (fggWindRes),
- Mouse, /* mouse clicks */
- nil, /* key clicks */
- Update, /* updates */
- Activate, /* activate/deactivate events */
- nil, /* close window */
- DoWClobber, /* dispose of window */
- nil, /* idle proc */
- false); /* irrelevant, since no idle proc */
-
- donors = NewBlobSet ();
- receptors = NewBlobSet ();
-
- fox = MakeDonor (foxIconRes); /* create fox, goose, grain blobs */
- goose = MakeDonor (gooseIconRes);
- grain = MakeDonor (grainIconRes);
- MakeBankBlobs (left, 50); /* make riverbank blobs - left MUST be 1st */
- MakeBankBlobs (right, 288);
- MakeFarmerBlob (); /* create Farmer blob - MUST follow banks */
- whew = MakeDonor (whewIconRes); /* create "Whew!" blob */
- rats = MakeDonor (ratsIconRes); /* create "Rats!" blob */
-
- /*
- * both controls are at same position - only one is shown at a time.
- */
-
- SetRect (&r, 135, 10, 235, 30);
- moveCtl =
- NewControl (wind, &r, "\pMove Right", false, 0, 0, 0, pushButProc, 0L);
- restartCtl =
- NewControl (wind, &r, "\pRestart", false, 0, 0, 0, pushButProc, 0L);
-
- Restart ();
-
- MakeFrontWind (wind);
- }
-